package kickboxing.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import kickboxing.controller.NewsyManager;
import kickboxing.model.Newsy;
import kickboxing.model.Uzytkownik;

public class NewsyEdycjaAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public NewsyEdycjaAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession sesja = request.getSession(true);
		Uzytkownik user = (Uzytkownik) sesja.getAttribute("user");
		Newsy news = new Newsy();
		NewsyManager linkController = new NewsyManager();
		
		if(user != null && user.getPrawa() == 'a'){
			try {
				BeanUtils.populate(news, request.getParameterMap());
				System.out.println("Spa" + news.getId_newsu() + " " + news.getTresc());
				linkController.edytujNewsa(news);
				
				String message = "Edytowano newsa";
				request.setAttribute("message", message);
				String x = "?message="+message;
				
				response.sendRedirect("admin/adminPanel.jsp"+x);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}else{
			response.sendError(404, "Brak Uprawinien");
		}
		
	}

}
